package cn.bjxy.shishaolong.system.profession_course.controller;


import cn.bjxy.shishaolong.common.ResultMsg;
import cn.bjxy.shishaolong.common.StatusCode;
import cn.bjxy.shishaolong.common.SysConst;
import cn.bjxy.shishaolong.common.Utils;
import cn.bjxy.shishaolong.system.profession_course.entity.Course;
import cn.bjxy.shishaolong.system.profession_course.service.ICourseService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 时少龙
 * @since 2019-03-12
 */
@CrossOrigin
@RestController
@RequestMapping("/api/admin/pc/course")
public class CourseController {

    @Autowired
    private ICourseService courseService;

    /**
     * 那条件查询
     *
     * @param param
     * @return
     */
    @RequestMapping("/queryByCondition")
    public ResultMsg queryByCondition(@RequestBody JSONObject param) {
        try {
            //查询条件
            Course course = null;
            if (param.containsKey(SysConst.QUERY_CONDITION_KEY)) {
                course = param.getObject(SysConst.QUERY_CONDITION_KEY, Course.class);
            }
            //分页查询
            if (param.containsKey(SysConst.PAGA_CURRENT_KEY) && param.containsKey(SysConst.PAGA_SIZE_KEY)) {
                IPage<Course> page = new Page<>();
                page.setCurrent(param.getLong(SysConst.PAGA_CURRENT_KEY)).setSize(param.getLong(SysConst.PAGA_SIZE_KEY));
//                List<Course> list = courseService.list();
                IPage<Course> list = courseService.queryByCondition(page, course);
                return ResultMsg.success(list);
            } else {  //普通查询
                List<Course> list = courseService.queryByCondition(course);
                return ResultMsg.success(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.QUERY_FAILURE, StatusCode.QUERY_FAILURE_MSG);
        }
    }

    /**
     * 保存
     *
     * @param course
     * @return
     */
    @PostMapping("/save")
    public ResultMsg addAdmin(@RequestBody Course course) {
        try {
            if (course == null) {
                return ResultMsg.create(StatusCode.PARAMETER_ERROR, StatusCode.PARAMETER_ERROR_MSG);
            }
            // 给修改日期赋值
            course.setUpdateAt(LocalDateTime.now());
            courseService.saveOrUpdate(course);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.SAVE_FAILURE, StatusCode.SAVE_FAILURE_MSG);
        }
    }

    /**
     * 删除
     *
     * @param param
     * @return
     */
    @DeleteMapping("/delect")
    public ResultMsg delectAdmin(@RequestBody JSONObject param) {
        try {
            if (param == null || !param.containsKey(SysConst.BATCH_ID)
                    || !param.getString(SysConst.BATCH_ID).contains(SysConst.BATCH_ID_SEPARATOR)) {
                return ResultMsg.create(StatusCode.PARAMETER_ERROR, StatusCode.PARAMETER_ERROR_MSG);
            }
            String ids = param.getString(SysConst.BATCH_ID);
            List idlist = Utils.idsToList(ids, SysConst.BATCH_ID_SEPARATOR);
            courseService.removeByIds(idlist);
            return ResultMsg.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.create(StatusCode.DELETE_FAILUER, StatusCode.DELETE_FAILUER_MSG);
        }
    }

}
